import 'package:erp/data/api/sale_project_api.dart';
import 'package:erp/data/model/page.dart';
import 'package:erp/main_state.dart';
import 'package:erp/routes/app_pages.dart';
import 'package:erp/widgets/form1/field/filter_chip.dart';
import 'package:erp/widgets/page_list/page_list_control.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

class SaleProjectListController extends PageListControl<dynamic> {
  final Map<int, String> contractStatusNameMap = {
    0: "待签订",
    1: "已签订",
    2: "已结案",
  };

  List<FilterChipItem<int>> get contractStatusItems => [
        FilterChipItem<int>(label: Text(contractStatusNameMap[0] ?? ""), data: 0),
        FilterChipItem<int>(label: Text(contractStatusNameMap[1] ?? ""), data: 1),
        FilterChipItem<int>(label: Text(contractStatusNameMap[2] ?? ""), data: 2),
      ];

  late final Map<String, String> areaDeptTranslation;
  late final Map<String, String> projectCategoryTranslation;

  late final List<FilterChipItem<String>> projectCategoryItems = [];
  late final List<FilterChipItem<String>> bidStatusItems = [];

  // control overlay
  var showOverlay = false.obs;

  // query param
  String keyWord = "";

  RxMap<String, dynamic> queryForm = RxMap<String, dynamic>({
    'projectCategory': null,
    'bidStatus': null,
    'contractStatus': null,
    'applyDate': null,
  });

  get queryParam => {
        ...queryForm,
        "keyWord": keyWord,
      };

  final TextEditingController textEditingController = TextEditingController();

  @override
  void onInit() {
    MainState ms = Get.find<MainState>();
    projectCategoryItems.addAll(ms
        .getOptions("project_category")
        .map((e) => FilterChipItem<String>(label: Text(e.label), data: e.value as String))
        .toList());
    bidStatusItems.addAll(ms
        .getOptions("bid_status")
        .map((e) => FilterChipItem<String>(label: Text(e.label), data: e.value as String))
        .toList());
    this.areaDeptTranslation = ms.getTranslation('area_dept');
    this.projectCategoryTranslation = ms.getTranslation('project_category');
    super.onInit();
  }

  @override
  Future<PageData> loadPageData({required int pageIndex}) async {
    var data = await SaleProjectApi.pageSaleProject(queryParam, pageIndex);
    List<dynamic> records = data["records"];
    var _list = records.map((e) {
      Map<String, dynamic> _map = e as Map<String, dynamic>;
      _map['areaDeptName'] = areaDeptTranslation[e['areaDept']];
      _map['projectCategoryName'] = projectCategoryTranslation[e['projectCategory']];
      return _map;
    }).toList();
    return PageData(records: _list, size: data["size"]);
  }

  void search(String value) {
    this.keyWord = value;
    this.showOverlay.value = false;
    easyRefreshController.callRefresh();
  }

  void tapDetail(Map<String, dynamic> item) async {
    String saleProjectNo = item["saleProjectNo"];
    var data = await SaleProjectApi.selectSaleProjectVoBySaleProjectNo(saleProjectNo);
    Get.toNamed(Routes.SALE_PROJECT, arguments: data);
  }
}
